package org.benben.shortlink.project.config;

import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import java.util.ArrayList;

/**
 * Sentinel初始化限流配置
 * @author benben
 */
@Component
public class SentinelRuleConfig implements InitializingBean {

    /**
     * 初始化限流规则
     * @throws Exception
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        ArrayList<FlowRule> rules = new ArrayList<>();
        FlowRule createOrderRule = new FlowRule();
        createOrderRule.setResource("create_short-link");
        //设置限流模式
        createOrderRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        //设置访问上限
        createOrderRule.setCount(1);
        rules.add(createOrderRule);
        //加载限流规则
        FlowRuleManager.loadRules(rules);
    }

}
